﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Controller.cs" company="DHGMS Solutions">
//   Copyright (c) 2012 DHGMS Solutions
//
//   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
//   The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
// <summary>
//   Controller for managing the donkey work
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.Kettle.Launcher
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;

    using Dhgms.Kettle.Launcher.Model.Task;

    using Microsoft.Win32;

    /// <summary>
    /// Controller for managing the donkey work
    /// </summary>
    public static class Controller
    {
        /// <summary>
        /// Entry point
        /// </summary>
        /// <param name="s">
        /// The source of the event
        /// </param>
        /// <param name="args">
        /// Arguments for the task
        /// </param>
        public static void Execute(object s, DoWorkEventArgs args)
        {
            Debug.WriteLine("Starting Execute");
            var steamInstallDirectory = GetInstallationDirectory();
            new EnsureSteamNotRunning(steamInstallDirectory).Execute();
            new Defrag(steamInstallDirectory).Execute();
            new SetAutoUpdateFlag().Execute();
            new StartSteam(steamInstallDirectory).Execute();
            Debug.WriteLine("Finished Execute");
        }

        /// <summary>
        /// Gets where steam is installed
        /// </summary>
        /// <returns>
        /// The steam installation directory.
        /// </returns>
        private static string GetInstallationDirectory()
        {
            // const string RegKeyName32 = @"HKEY_LOCAL_MACHINE\SOFTWARE\Valve\Steam";
            const string RegKeyName64 = @"SOFTWARE\Wow6432Node\Valve\Steam";
            var subkey = Registry.LocalMachine.OpenSubKey(RegKeyName64, false);
            if (subkey == null)
            {
                throw new ApplicationException("Unable to open registry key");
            }

            var installationPathValue = subkey.GetValue("InstallPath");
            if (installationPathValue == null)
            {
                throw new ApplicationException("Unable to retrieve installation directory for Steam.");
            }

            if (installationPathValue.GetType() != typeof(string))
            {
                throw new ApplicationException("Unable to retrieve installation directory for Steam.");
            }

            var result = (string)installationPathValue;

            if (!result.EndsWith(@"\"))
            {
                result = result + @"\";
            }

            return result;
        }
    }
}
